package com.achun.getcouponleague.presenter.impl;

import com.achun.getcouponleague.Bean.CategoriesContent;
import com.achun.getcouponleague.base.IBasePresenter;
import com.achun.getcouponleague.model.ServiceApi;
import com.achun.getcouponleague.presenter.ICategoryPagePresenter;
import com.achun.getcouponleague.utils.Logutils;
import com.achun.getcouponleague.utils.RetrogitManager;
import com.achun.getcouponleague.view.ICategoryPageViewCallback;
import com.achun.getcouponleague.view.IHomeCallback;

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class CategoryPagePresenter implements ICategoryPagePresenter {
private List<ICategoryPageViewCallback> callbacks=new ArrayList<>();
private HashMap<Integer,Integer> updataPage=new HashMap<>();
private int targetpage=1;
//单例模式
    private CategoryPagePresenter(){

    }
    private static CategoryPagePresenter categoryPagePresenter=null;
    public static CategoryPagePresenter getInstance(){
        if (categoryPagePresenter == null) {
            synchronized (CategoryPagePresenter.class){
                if (categoryPagePresenter == null) {
                    categoryPagePresenter=new CategoryPagePresenter();
                }
            }
        }
        return categoryPagePresenter;
    }
    @Override
    public void registerCallback(ICategoryPageViewCallback callback) {
        if (!callbacks.contains(callback)) {
            callbacks.add(callback);
        }
    }

    @Override
    public void unRegisterCallback(ICategoryPageViewCallback callback) {
        if (callbacks.contains(callback)) {
            callbacks.remove(callback);
        }
    }

    @Override
    public void getContentByCategroyId(int materialId) {
        Integer currentpage = updataPage.get(materialId);
        if (currentpage==null){
            targetpage=1;
            updataPage.put(materialId,currentpage);
        }

        Logutils.e(this.getClass(),"当前页码为"+targetpage);
        //加载状态

        for (ICategoryPageViewCallback callback : callbacks) {
            if (materialId==callback.getCategoryId()){
                callback.onLoading();
            }
        }
        Call<CategoriesContent> task = getCategoriesContentCall(materialId);
        task.enqueue(new Callback<CategoriesContent>() {
            @Override
            public void onResponse(Call<CategoriesContent> call, Response<CategoriesContent> response) {
                int code = response.code();
                if (code== HttpURLConnection.HTTP_OK){
                    CategoriesContent categoriesContent = response.body();
                    if (categoriesContent==null||categoriesContent.getData().size()==0){
                        //数据为空
                       handleOnEmpty(materialId);
                    }else {
                        //成功处理
                        Logutils.e(this.getClass(),"success--->"+categoriesContent.toString());
                        handleOnLoaded(categoriesContent,materialId);
                        targetpage++;
                        updataPage.put(materialId,targetpage);
                    }
                }else {
                    //错误处理
                    handleOnError(materialId);

                }
            }

            @Override
            public void onFailure(Call<CategoriesContent> call, Throwable t) {
                //错误处理
                handleOnError(materialId);
                Logutils.e(this.getClass(),t.toString());
            }
        });
    }

    private Call<CategoriesContent> getCategoriesContentCall(int materialId) {
        Retrofit retrofit = RetrogitManager.getInstance().getRetrofit();
        ServiceApi serviceApi = retrofit.create(ServiceApi.class);
        Call<CategoriesContent> task = serviceApi.getCategoryContent(materialId, targetpage);
        return task;
    }

    private void handleOnError(int materialId) {
        for (ICategoryPageViewCallback callback : callbacks) {
            if (materialId==callback.getCategoryId()){
                callback.onError();
            }
        }

    }

    private void handleOnLoaded(CategoriesContent categoriesContent, int materialId) {
        List<CategoriesContent.DataBean> datas = categoriesContent.getData();
        for (ICategoryPageViewCallback callback : callbacks) {
            if (materialId==callback.getCategoryId()){
                callback.onCOntentLoaded(datas);
                //轮播图数据
                List<CategoriesContent.DataBean> loopBannerDatas = datas.subList(datas.size() - 6, datas.size() - 1);
                callback.onLooperListLoaded(loopBannerDatas);
            }
        }
    }

    private void handleOnEmpty(int materialId) {
        for (ICategoryPageViewCallback callback : callbacks) {
            if (materialId==callback.getCategoryId()){
                callback.onEmpty();
            }
        }
    }

    @Override
    public void loadingMore(int materialId) {
        Integer currentpage = updataPage.get(materialId);
        if (currentpage==null){
            targetpage=1;
            updataPage.put(materialId,currentpage);
        }
            targetpage++;
        updataPage.put(materialId,targetpage);

        Logutils.e(this.getClass(),"加载更多：当前页码为"+targetpage);
        Call<CategoriesContent> task = getCategoriesContentCall(materialId);
        task.enqueue(new Callback<CategoriesContent>() {
            @Override
            public void onResponse(Call<CategoriesContent> call, Response<CategoriesContent> response) {
                int code = response.code();
                if (code==HttpURLConnection.HTTP_OK){
                    CategoriesContent categoriesContent = response.body();
                    handleLoadMoreOnLoad(categoriesContent,materialId);
                }else {
                    //错误处理
                    handleLoadMoreOnError(materialId);

                }
            }

            @Override
            public void onFailure(Call<CategoriesContent> call, Throwable t) {
                //错误处理
                handleLoadMoreOnError(materialId);
            }
        });


    }

    private void handleLoadMoreOnError(int materialId) {
       targetpage--;
        updataPage.put(materialId,targetpage);
        for (ICategoryPageViewCallback callback : callbacks) {
            if (callback.getCategoryId()==materialId)
                callback.onLoadMoreError();
        }
    }

    private void handleLoadMoreOnLoad(CategoriesContent categoriesContent,int categoryId) {
        if (categoriesContent==null||categoriesContent.getData().size()==0){
            for (ICategoryPageViewCallback callback : callbacks) {
                if (callback.getCategoryId()==categoryId){
                    callback.onLoadMoreEmpty();
                }
            }
        }else {
            for (ICategoryPageViewCallback callback : callbacks) {
                if (callback.getCategoryId()==categoryId){
                    callback.onLoadMoreLoaded(categoriesContent.getData());
                    Logutils.e(this.getClass(),"加载更多数据啦");
                }
            }

        }
    }

    @Override
    public void reload(int categoryId) {

    }
}
